package com.example.mardan_demo1.service.impl;

import com.example.mardan_demo1.mapper.YaopinMapper;
import com.example.mardan_demo1.pojo.*;

import com.example.mardan_demo1.pojo.hzp.*;
import com.example.mardan_demo1.service.YaopinService;
import com.example.mardan_demo1.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class YaopinServiceImpl implements YaopinService {

    @Autowired
    private YaopinMapper yaopinMapper;
    @Autowired
    private YaopinService yaopinService;

    @Override
    public Result getJianYanXiangMuByJianPinId(String jianPinDengJiId){
        PageBean<SearchList> pb = new PageBean<>();
        PageHelper.startPage(1, 100);
        List<SearchList> as = yaopinMapper.getJianYanXiangMuByJianPinId(jianPinDengJiId);
        Page<SearchList> p = (Page<SearchList>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    @Override
    public Result houxuanlinst(String name, String jianPinLeiBie) {
        PageBean<SearchList> pb = new PageBean<>();
        PageHelper.startPage(1, 100);
        List<SearchList> as =  yaopinMapper.houxuanlist(name, jianPinLeiBie);
        Page<SearchList> p = (Page<SearchList>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    public Result<PageBean<Yaopin>> list(Integer pageNum, Integer pageSize, String yangPinMingCheng){
        // 判断权限
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userID = (Integer) userInfo.get("userID");
        String userRole = (String) userInfo.get("userRole");
        if (!userRole.equals("customer")) {
            return Result.error("权限不够");
        }
        // 创建pageBean对象
        PageBean<Yaopin> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<Yaopin> as = yaopinMapper.list(userID, yangPinMingCheng);
        // 数据列表和总数、页数合并
        Page<Yaopin> p = (Page<Yaopin>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    public Result add(Yaopin yaopin) {
//         状态设置为暂存
        yaopin.setZhuang_tai("暂存");
//         创建日期
        yaopin.setChuang_jian_shi_jian(LocalDateTime.now());
        // 修改日期
        yaopin.setXiu_gai_shi_jian(LocalDateTime.now());
        // 创建人
        Map<String, Object> map = ThreadLocalUtil.get();
        yaopin.setChuang_jian_ren(String.valueOf(map.get("id")));
        System.out.println("样品数量" + yaopin.getYang_pin_shu_liang());
        yaopinMapper.add(yaopin);
        return  Result.success("添加成功");
    }
    public Result hzpFenLeiAdd(HuaZhuangPinFenLei fenlei){
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        String yongHuMing = userInfo.get("username").toString();
        fenlei.setChuangJianRen(yongHuMing);
        yaopinMapper.hzpFenLeiAdd(fenlei);
        return Result.success("操作成功");
    }
    public Result hzpXiangMuAdd(HuaZhuangPinJianYanXiangMu jianYanXiangMu){
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        String yongHuMing = userInfo.get("username").toString();
        jianYanXiangMu.setChuangJianRen(yongHuMing);
        yaopinMapper.hzpXiangMuAdd(jianYanXiangMu);
        return Result.success("操作成功");
    }
    public Result hzpFenLeiList(Integer pageSize, Integer pageNum){
        // 创建pageBean对象
        PageBean<HuaZhuangPinFenLei> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HuaZhuangPinFenLei> as = yaopinMapper.hzpFenLeiList();
        // 数据列表和总数、页数合并
        Page<HuaZhuangPinFenLei> p = (Page<HuaZhuangPinFenLei>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    public Result hzpXiangMuList(String xiangMuMingCheng, Integer pageSize, Integer pageNum){
        // 创建pageBean对象
        PageBean<HuaZhuangPinJianYanXiangMu> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HuaZhuangPinJianYanXiangMu> as = yaopinMapper.hzpXiangMuList(xiangMuMingCheng);
        // 数据列表和总数、页数合并
        Page<HuaZhuangPinJianYanXiangMu> p = (Page<HuaZhuangPinJianYanXiangMu>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    @Transactional
    public Result hzpXiangMuGuanLianDaLei( Integer xiangMuId, Integer[] leiBieIds){
        // 查看是否存在id对应的项目
        HuaZhuangPinJianYanXiangMu xiangMu = yaopinMapper.findXiangMuById(xiangMuId);
        if( xiangMu == null) { return  Result.error("该项目不存在!");}
        // 如果类别ids为空，那就清空，也就是说删除所有的关联
        if (leiBieIds.length == 0) {
            // 删除所有的相关关联
            yaopinMapper.shanChuGuanLianByXiangMuId(xiangMuId);
            return Result.success("修改成功");
        }
        // 查看ids对应的分类是否存在
        List<HuaZhuangPinFenLei> fenLeiList = yaopinMapper.findfenLeisByIds(leiBieIds);
        if(fenLeiList.size() != leiBieIds.length){return Result.error("所选的大类数组异常，请联系管理员");}
        // 关联之前先删除再新增不就行了吗
        yaopinMapper.shanChuGuanLianByXiangMuId(xiangMuId);
        // 现在用事物一个一个关联,先检查是否已存在关联记录，如果存在就不重复关联
        for(int i = 0; i < leiBieIds.length; i++) {
            GuanLianFenLeiHeXiangMu guanLian =  yaopinMapper.findGuanLianByXiangMuIdAndLeiBieId(xiangMuId, leiBieIds[i]);
            if (guanLian == null) {
                yaopinMapper.xinZengGuanLian(xiangMuId, leiBieIds[i]);
            }
        }
        return Result.success("操作成功");
    }
    public Result hzpXiangMuListWithDaLei(String xiangMuMingCheng,  Integer pageSize,  Integer pageNum){
        // 创建pageBean对象
        PageBean<HuaZhuangPinJianYanXiangMu> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HuaZhuangPinJianYanXiangMu> as = yaopinMapper.hzpXiangMuList(xiangMuMingCheng);
        // 数据列表和总数、页数合并
        Page<HuaZhuangPinJianYanXiangMu> p = (Page<HuaZhuangPinJianYanXiangMu>) as;
        List<DaLeiInXiangMu> daLeiInXiangMuList = new ArrayList<DaLeiInXiangMu>();
        for(int i =0; i < p.getResult().size(); i++) {
            System.out.println(p.getResult().get(i));
            DaLeiInXiangMu item = new DaLeiInXiangMu();
            item.setXiangMu(p.getResult().get(i));
            item.setFenLeiList(getGuanLianFenLeiByXiangMuId(p.getResult().get(i).getId()));
            daLeiInXiangMuList.add(item);
        }
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(daLeiInXiangMuList);
    }
    // 根据项目id获取所述的分类
    public List<GuanLianFenLeiHeXiangMu> getGuanLianFenLeiByXiangMuId(Integer xiangMuId) {
        return yaopinMapper.getGuanLianFenLeiByXiangMuId(xiangMuId);
    }

    public  Result hzpGetXiangMuListByDaleiId(Integer fenLeiId,  Integer pageSize,  Integer pageNum) {
        // 创建pageBean对象
        PageBean<HuaZhuangPinJianYanXiangMu> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HuaZhuangPinJianYanXiangMu> as = yaopinMapper.hzpGetXiangMuListByDaleiId(fenLeiId);
        // 数据列表和总数、页数合并
        Page<HuaZhuangPinJianYanXiangMu> p = (Page<HuaZhuangPinJianYanXiangMu>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }@Transactional
    public Result hzpXinZengYangPin(HzpInfo hzpInfo,String zhuangTai){
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        String userRole =(String) userInfo.get("role");
        if(!userRole.equals("customer")) {
            return Result.error("只有客户才能添加物品");
        }
        Integer userId = (Integer) userInfo.get("id");
        hzpInfo.setCreator(userId.toString());
        if(zhuangTai.equals("暂存")) {
            hzpInfo.setZhuangTai("暂存");
        }
        if (zhuangTai.equals("提交")) {
            hzpInfo.setZhuangTai("提交");
        }
        String caoZuoLeiXing = hzpInfo.getZhuangTai();
        String chuLiYiJian = hzpInfo.getZhuangTai();
        Integer xinZengYangPinId = yaopinMapper.hzpXinZengYangPin(hzpInfo);
        Long temp = hzpInfo.getId();
        Integer yangPinId = temp != null ? temp.intValue() : null; // 转换为 Integer
        yaopinMapper.xinZengHzpChuLiRiZhi(yangPinId,userId, caoZuoLeiXing, chuLiYiJian, "H" );
        return Result.success(hzpInfo.getId());
    }
    public Result khGetHzpList(String yangPinMingCheng, Integer pageSize, Integer pageNum){
        // 获取客户的id
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        System.out.println("userId=" + userId);
        // 创建pageBean对象
        PageBean<HzpInfo> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HzpInfo> as = yaopinMapper.khGetHzpList(yangPinMingCheng, userId);
        // 数据列表和总数、页数合并
        Page<HzpInfo> p = (Page<HzpInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    public Result adminGetHzpList(String yangPinMingCheng, Integer pageSize, Integer pageNum){
        // 创建pageBean对象
        PageBean<HzpInfo> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<HzpInfo> as = yaopinMapper.adminGetHzpList(yangPinMingCheng);
        // 数据列表和总数、页数合并
        Page<HzpInfo> p = (Page<HzpInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    public Result adminGetHzpDetailById(Integer id){
        HzpInfo hzpInfo = yaopinMapper.adminGetHzpDetailById(id);
        return Result.success(hzpInfo);
    }
    public Result adminGetHzpXiangMuByIds(int[] ids){
        // 创建pageBean对象
        PageBean<HuaZhuangPinJianYanXiangMu> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<HuaZhuangPinJianYanXiangMu> as = yaopinMapper.adminGetHzpXiangMuByIds(ids);
        // 数据列表和总数、页数合并
        Page<HuaZhuangPinJianYanXiangMu> p = (Page<HuaZhuangPinJianYanXiangMu>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);

    }
    @Transactional
    public  Result adminShouLiHzp(Integer yangPinId, String caoZuoLeiXing, String chuLiYiJian, String jianPinBianHao){
        // 查看样品状态是否为提交
        HzpInfo tempInfo = yaopinMapper.findHzpInfoById(yangPinId);
        if(tempInfo == null){return Result.error("该数据不存在");}
        if(!tempInfo.getZhuangTai().equals("提交")) {
            return Result.error("该样品状态为" + tempInfo.getZhuangTai() + ",无法受理");
        }
        // 获取操作人的id
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        String yongHuId = userId.toString();
        HzpInfo hzpInfo = new HzpInfo();
        hzpInfo.setId(yangPinId);
        hzpInfo.setZhuangTai("受理");
        hzpInfo.setJianPinBianHao(jianPinBianHao);
        yaopinMapper.kehuUpdateHzpInfo(hzpInfo);
        caoZuoLeiXing = "受理";
        yaopinMapper.adminShouLiHzp(yangPinId,caoZuoLeiXing,chuLiYiJian,yongHuId, "H");
        return Result.success("受理成功");

    }
    public Result kehuUpdateHzpInfo(HzpInfo hzpInfo){
        // 获取操作人的id
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        hzpInfo.setCreator(userId.toString());
        hzpInfo.setZhuangTai("暂存");
        yaopinMapper.kehuUpdateHzpInfo(hzpInfo);
        return Result.success("保存成功");
    }
    public Result getHzpJinDuById(Integer id){
        // 创建pageBean对象
        PageBean<HzpJinDu> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<HzpJinDu> as = yaopinMapper.getHzpJinDuById(id);
        // 数据列表和总数、页数合并
        Page<HzpJinDu> p = (Page<HzpJinDu>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    @Transactional
    public  Result adminBoHuiHzpShouLi(Integer yangPinId, String chuLiYiJian) {
        // 获取操作人的id
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        String yongHuId = userId.toString();
        HzpInfo hzpInfo = yaopinMapper.findHzpInfoById(yangPinId);
        if(hzpInfo == null) {
            return Result.error("该数据不存在");
        }
        // 更新登记表中的状态
        hzpInfo.setZhuangTai("暂存");
        yaopinMapper.kehuUpdateHzpInfo(hzpInfo);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yangPinId);
        log.setYongHuId(Long.parseLong(yongHuId));
        log.setCaoZuoLeiXing("驳回");
        log.setYangPinLeiXing("H");
        log.setChuLiYiJian(chuLiYiJian);
        yaopinMapper.addHzpProcessLog(log);
        return Result.success("驳回成功");
    }
    @Transactional
    public  Result adminUploadBgsFujian(Integer yangPinId, String baoGaoShuFuJian){
        // 获取操作人的id
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        String yongHuId = userId.toString();
        // 根据yangPinId获取样品信息
        HzpInfo tempInfo = yaopinMapper.findHzpInfoById(yangPinId);
        if(tempInfo == null){return Result.error("该数据不存在");}
        tempInfo.setBaoGaoShuFuJian(baoGaoShuFuJian);
        tempInfo.setZhuangTai("完成");
        yaopinMapper.kehuUpdateHzpInfo(tempInfo);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yangPinId);
        log.setYongHuId(Long.parseLong(yongHuId));
        log.setCaoZuoLeiXing("上传报告书");
        log.setChuLiYiJian(baoGaoShuFuJian);
        log.setYangPinLeiXing("H");
        yaopinMapper.addHzpProcessLog(log);
        return Result.success("上传报告书成功");
    }
    @Transactional
    public Result keHuTiJiao(HzpInfo hzpInfo){
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        String yongHuId = userId.toString();
        // 查看是否存在
        HzpInfo tempHzpInfo = yaopinMapper.findHzpInfoById((int) hzpInfo.getId());
        if(tempHzpInfo == null){return Result.error("该数据不存在");}
        // 提交
        tempHzpInfo.setZhuangTai("提交");
        yaopinMapper.kehuUpdateHzpInfo(tempHzpInfo);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYongHuId(Long.parseLong(yongHuId));
        log.setChuLiYiJian("申请送检");
        log.setCaoZuoLeiXing("申请送检");
        log.setYangPinLeiXing("H");
        log.setYangPinId(hzpInfo.getId());
        yaopinMapper.addHzpProcessLog(log);
        return Result.success("申请送检成功");
    }


}
